<template>
  <div class="user">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/" style="line-hegiht:45px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索栏 -->
    <div class="search-box">
      <el-input placeholder="请输入内容" v-model="searchLang" class="input-with-select" @keyup.enter.native="searchUser" style="width:300px;margin-right:10px;">
        <el-button slot="append" icon="el-icon-search" @click="searchUser"></el-button>
      </el-input>
      <el-button type="success" plain @click="adddialogFormVisible = true">添加用户</el-button>
    </div>
    <!-- 表单 -->
    <template>
      <el-table
        :data="userList"
        border
        style="width: 100%;margin-top:15px">
        <el-table-column
        type="index"
        width="50"></el-table-column>
        <el-table-column
          prop="username"
          label="姓名"
          width="180">
        </el-table-column>
        <el-table-column
          prop="email"
          label="邮箱"
          width="180">
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="电话">
        </el-table-column>
        <el-table-column
          prop="address"
          label="状态">
          <template slot-scope="scope">
            <el-switch
              @change="typeBtn(scope.row)"
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <el-button type="primary" plain icon="el-icon-edit" @click="showEditForm(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
              <el-button type="primary" plain icon="el-icon-delete" @click="delSubmit(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="授权角色" placement="top">
              <el-button type="primary" plain icon="el-icon-share" @click="showRoleForm(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
     <el-pagination
       @size-change="handleSizeChange"
       @current-change="handleCurrentChange"
       :current-page="currentPage"
       :page-sizes="[10, 20, 30, 40]"
       :page-size="10"
       layout="total, sizes, prev, pager, next, jumper"
       :total="userNum"
       style="background:#eeeeee;margin-top:15px;">
     </el-pagination>
    </template>
    <!-- 添加用户 -->
    <el-dialog title="添加用户" :visible.sync="adddialogFormVisible">
      <el-form :model="addform" label-width="80px" :rules="rules" ref="addform">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addform.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addform.password" type="password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addform.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="addform.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="adddialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubmitForm('addform')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑用户 -->
    <el-dialog title="编辑用户" :visible.sync="editdialogFormVisible">
      <el-form :model="editform" label-width="80px" :rules="rules" ref="editform">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editform.username" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editform.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="editform.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editdialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubmitForm('editform')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配用户角色 -->
    <el-dialog title="授权角色" :visible.sync="roledialogFormVisible">
      <el-form :model="roleform" label-width="80px" :rules="rules" ref="roleform">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="roleform.username" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" >
          <template>
            <el-select v-model="value" placeholder="请选择" @change='changeRole'>
              <el-option
                v-for="item in rolelist"
                :key="item.id"
                :label="item.roleName"
                :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="roledialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="roleSubmitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getUsersList, addUser, putUser, editUser, delUser, getRoleList, roleUser} from '@/api/index.js'
export default {
  data () {
    return {
      searchLang: '',
      userList: [],
      currentPage: 1,
      userNum: 1,
      pageSize: 10,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
        ],
        mobile: [
          { required: true, message: '电话不能为空' }
        ]
      },
      adddialogFormVisible: false,
      addform: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      editdialogFormVisible: false,
      editform: {
        id: '',
        username: '',
        email: '',
        mobile: ''
      },
      deldialogVisible: false,
      roledialogFormVisible: false,
      roleform: {
        id: '',
        rid: '',
        rolename: ''
      },
      value: '',
      rolelist: []
    }
  },
  mounted () {
    this.getAllUsers()
  },
  methods: {
    // 授权角色按钮提交
    roleSubmitForm () {
      if (this.value === '') {
        this.$message.error('请选择角色属性')
        return false
      }
      roleUser(this.roleform).then(res => {
        if (res.meta.status === 200) {
          this.$message.success(res.meta.msg)
          this.roledialogFormVisible = false
        } else {
          this.$message.error(res.meta.msg)
          this.roledialogFormVisible = false
          return false
        }
      })
    },
    // 选择角色时候触发的事件，用于获取角色id
    changeRole () {
      this.roleform.rid = this.value
    },
    // 打开授权角色框
    showRoleForm (value) {
      this.roledialogFormVisible = true
      this.roleform.id = value.id
      this.roleform.username = value.username
      getRoleList().then(res => {
        this.rolelist = res.data
      })
    },
    // 删除用户提交
    delSubmit (value) {
      this.$confirm(`是否删除id为${value.id}且用户名为${value.username}的用户`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delUser(value.id).then(res => {
          console.log(res)
          if (res.meta.status === 200) {
            this.deldialogVisible = false
            this.$message.success(res.meta.msg)
            this.getAllUsers()
          } else {
            this.deldialogVisible = false
            this.$message.error(res.meta.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑用户提交
    editSubmitForm () {
      editUser(this.editform).then(res => {
        if (res.meta.status === 200) {
          this.editdialogFormVisible = false
          this.$message.success(res.meta.msg)
          this.getAllUsers()
        } else {
          this.editdialogFormVisible = false
          this.$message.error(res.meta.msg)
        }
      })
    },
    // 显示编辑用户框
    showEditForm (value) {
      this.editdialogFormVisible = true
      this.editform.id = value.id
      this.editform.username = value.username
      this.editform.email = value.email
      this.editform.mobile = value.mobile
    },
    // 修改用户状态
    typeBtn (value) {
      putUser({uId: value.id, type: value.mg_state}).then(res => {
        if (res.meta.status === 200) {
          this.$message.success(res.meta.msg)
        }
      })
    },
    // 添加用户
    addSubmitForm (addform) {
      this.$refs[addform].validate((valid) => {
        if (valid) {
          addUser(this.addform).then(res => {
            if (res.meta.status === 201) {
              this.adddialogFormVisible = false
              this.$refs[addform].resetFields()
              this.$message.success(res.meta.msg)
              this.getAllUsers()
            } else {
              this.$message.error(res.meta.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    // 查询用户
    searchUser () {
      this.getAllUsers()
    },
    // 获取全部用户
    getAllUsers () {
      getUsersList({query: this.searchLang, pagenum: this.currentPage, pagesize: this.pageSize}).then(res => {
        this.userNum = res.data.total
        this.userList = res.data.users
      })
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getAllUsers()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getAllUsers()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  background-color: #F5F5F5;
  height: 45px;
  font-size: 15px;
  padding-left: 10px;
  line-height: 45px;
  margin-bottom: 15px;
}
.user {
  .search-input {
    width: 300px;
  }
}
</style>
